//
//  CitySelectView.m
//  BigJoy
//
//  Created by Rowling on 2017/7/10.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "CitySelectView.h"
@interface CitySelectView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *pikerView;
@property (nonatomic,strong) NSArray *provinceArray;
@property (nonatomic,assign) NSInteger cityIndex;
@property (nonatomic,assign) NSInteger provinceIndex;
@property (nonatomic,assign) NSInteger areaIndex;
@end

@implementation CitySelectView

+(instancetype)fastLogin {
    return FastLogin;
}

-(void)setNoArea:(BOOL)noArea {
    _noArea = noArea;
    [self.pikerView reloadAllComponents];
}

-(void)awakeFromNib {
    [super awakeFromNib];
    [self loadJason];
    self.pikerView.delegate = self;
    self.pikerView.dataSource = self;
}

-(void)layoutSubviews {
    UILabel * lable1 = (UILabel *)[self.pikerView viewForRow: 0 forComponent:0];
    lable1.textColor = [UIColor redColor];
    UILabel * lable2 = (UILabel *)[self.pikerView viewForRow: 0 forComponent:1];
    lable2.textColor = [UIColor redColor];
    // 三级城市
    if (!self.noArea) {
        UILabel * lable3 = (UILabel *)[self.pikerView viewForRow: 0 forComponent:2];
        lable3.textColor = [UIColor redColor];
    }
}

-(void)loadJason {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"areaNew" ofType:@"json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:nil];
    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    self.provinceArray = dic[@"districts"][0][@"districts"];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (self.noArea) {
        return 2; // 两级
    }
    return 3; // 三级
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.provinceArray.count;
    }else if (component == 1) {
        return [self.provinceArray[self.provinceIndex][@"districts"] count] ;
    }else {
        return [self.provinceArray[self.provinceIndex][@"districts"][self.cityIndex][@"districts"] count];
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *pickerLabel = (UILabel *)view;
    if (pickerLabel == nil) {
        CGRect frame = CGRectMake(0.0, 0.0, 50, 60);
        pickerLabel = [[UILabel alloc] initWithFrame:frame];
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont systemFontOfSize:15.0f]];
    }
    if (component == 0)
    {
        pickerLabel.text = self.provinceArray[row][@"name"];
    }
    else if (component == 1)
    {
        pickerLabel.text = self.provinceArray[self.provinceIndex][@"districts"][row][@"name"];
    }
    else if (component == 2)
    {
        pickerLabel.text = self.provinceArray[self.provinceIndex][@"districts"][self.cityIndex][@"districts"][row][@"name"];
    }
    [pickerLabel sizeToFit];
    return pickerLabel;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 50;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.provinceIndex = row;
        [self.pikerView reloadAllComponents];
    }
    if (component == 1) {
        self.cityIndex = row;
        [self.pikerView reloadAllComponents];
    }
    if (component == 2) {
        self.areaIndex = row;
    }
    UILabel * lable1 = (UILabel *)[self.pikerView viewForRow: [pickerView selectedRowInComponent:0] forComponent:0];
    lable1.textColor = [UIColor redColor];
    UILabel * lable2 = (UILabel *)[self.pikerView viewForRow: [pickerView selectedRowInComponent:1] forComponent:1];
    lable2.textColor = [UIColor redColor];
    // 三级
    if (!self.noArea) {
        UILabel * lable3 = (UILabel *)[self.pikerView viewForRow: [pickerView selectedRowInComponent:2] forComponent:2];
        lable3.textColor = [UIColor redColor];
    }
}

- (IBAction)cancelButtonClick:(id)sender {
    [self removeFromSuperview];
}

- (IBAction)okbuttonClick:(id)sender {
    if (self.adBlock) {
        self.adBlock([NSString stringWithFormat:@"%@ %@ %@",self.provinceArray[
                      self.provinceIndex][@"name"],
                      self.provinceArray[self.provinceIndex][@"districts"][self.cityIndex][@"name"],
                      self.noArea ? @"" : self.provinceArray[self.provinceIndex][@"districts"][self.cityIndex][@"districts"][self.areaIndex][@"name"]]);
    }
    [self removeFromSuperview];
}

-(void)dealloc {
    NSLog(@"address picker dealloc");
}

@end
